<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible=true">添加分类</el-button>
      </div>
      <el-table
      :data="artList"
      style="width: 100%"
      stripe
      border>
      <el-table-column
        type=index
        label="编号"
        width='60'>
      </el-table-column>
      <el-table-column
        prop="cate_name"
        label="文章分类">
      </el-table-column>
      <el-table-column
        prop="cate_alias"
        label="文章类别">
      </el-table-column>
      <el-table-column
        label="功能按钮">
      <template v-slot='{row}'>
        <el-button type="primary" size="mini" @click="resOpen(row)">修改</el-button>
        <el-button type="info" size='mini' @click="remove(row)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    </el-card>
    <el-dialog
    :close-on-click-modal="false"
    title="提示"
    :visible.sync="addVisible"
    width="35%"
    @closed='$refs.addForm.resetFields()'>
      <el-form ref="addForm" :model="addForm" :rules="rules">
        <el-form-item label="文章分类" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类名称" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button type="primary" @click="pushArtList">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
      :close-on-click-modal="false"
      title="提示"
      :visible.sync="resVisible"
        width="35%"
        @closed='$refs.resForm.resetFields()'>
            <el-form ref="resForm" :model="resForm" :rules="rules">
        <el-form-item label="文章分类" prop="cate_name">
          <el-input v-model="resForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类名称" prop="cate_alias">
          <el-input v-model="resForm.cate_alias"></el-input>
        </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resVisible = false">取 消</el-button>
          <el-button type="primary" @click="resArtList">确 定</el-button>
        </span>
      </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      artList: [],
      addVisible: false,
      resVisible: false,
      resForm: {
        id: '',
        cate_name: '',
        cate_alias: ''
      },
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getArtList () {
      const { data: res } = await this.$axios.get('/my/cate/list')
      if (res.code === 0) {
        this.artList = res.data
        // console.log(this.artList)
      }
    },
    pushArtList () {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.addVisible = false
        this.getArtList()
      })
    },
    resOpen (data) {
      if (data.id === 1 || data.id === 2) return this.$message.error('不能修改此选项')
      this.resForm.id = data.id
      this.resVisible = true
    },
    remove (data) {
      if (data.id === 1 || data.id === 2) return this.$message.error('不能修改此选项')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete('/my/cate/del', { params: { id: data.id } })
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getArtList()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    resArtList () {
      this.$refs.resForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.put('/my/cate/info', this.resForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.resVisible = false
        this.getArtList()
      })
    }
  },
  created () {
    this.getArtList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
